//
//  ImageShowViewController.m
//  调用摄像头
//
//  Created by administrator on 12-9-12.
//  Copyright (c) 2012年 administrator. All rights reserved.
//

#import "ImageShowViewController.h"

@interface ImageShowViewController ()

@end

@implementation ImageShowViewController
@synthesize message;
@synthesize Image;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    
    UIBarButtonItem *rightButton = [[UIBarButtonItem alloc]
                                    initWithBarButtonSystemItem:UIBarButtonSystemItemAction
                                    target:self
                                    action:@selector(share)];
    self.navigationItem.rightBarButtonItem = rightButton;
    
}

- (void) share
{
    UIImageWriteToSavedPhotosAlbum(self.Image.image, self, @selector(imageSavedToPhotosAlbum: didFinishSavingWithError: contextInfo:), nil);
}
- (void)imageSavedToPhotosAlbum:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo {
    NSString *message;
    NSString *title;
    if (!error) {
        title = NSLocalizedString(@"分享成功", @"");
        message = NSLocalizedString(@"您的图片已分享到照片库", @"");
    } else {
        title = NSLocalizedString(@"储存失败", @"");
        message = [error description];
    }
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title
                                                    message:message
                                                   delegate:nil
                                          cancelButtonTitle:NSLocalizedString(@"OK", @"")
                                          otherButtonTitles:nil];
    [alert show];
    [alert release];
}





NSString *documentsFolder()
{
    return [NSHomeDirectory()
            stringByAppendingPathComponent:@"Documents/Image"];
}
// 加载图像
//path = [documentsFolder() stringByAppendingPathComponent:@"image.png"];
//return [UIImage imageWithContentsOfFile:path];


- (void)viewWillAppear:(BOOL)animated {
  //  detailLabel.text = message;////////////////////////////////////////////////////////////
    //UIImage *image = [UIImage imageNamed:message];
    //self.Image.image = [UIImage imageNamed: message];
    NSLog(@"%@",message);
    
    NSString *path = [documentsFolder() stringByAppendingPathComponent:message];
    
    float heigh=[UIImage imageWithContentsOfFile:path].size.height;
    float width=[UIImage imageWithContentsOfFile:path].size.width;
    
    if (width>320 || heigh>416) {
        self.Image.contentMode=UIViewContentModeScaleAspectFit;
    }
    else
    {
        self.Image.contentMode=UIViewContentModeCenter;
    }
    
    
    
    self.Image.image=[UIImage imageWithContentsOfFile:path];
    
    [super viewWillAppear:animated];
}



- (void)viewDidUnload
{
    self.message=nil;
    [self setImage:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (void)dealloc {
    [Image release];
    [super dealloc];
}
@end
